<style>
.pageFormContent .inputArea p {
	float: none;
	width: 800px;
}

#inStock .lb {
	width: 150px;
	left: 480px;
	position: absolute;
}

#inStock span.error {
	position: absolute !important;
	left: 260px;
}
</style>
<#include "/common/pagerForm.html">

<div class="pageContent">
	<div class="pageFormContent" id="inStock">
		<div class="inputArea">
			<form rel="pagerForm" method="post" action="" class="pageForm required-validate">
				<p>
					<label><@label key="workCenter.C,colon">工作中心：</@label></label>
					 <select id="workcenter" onchange="manualRequirementJs._workCenterChange(this)" class="allyselect required" ref="workUnit" postField="workcenterCode" refUrl="${rc.getContextPath()}/manage/workCenter/workCenterChange" name="workcenterCode" class="required">
						<option value=''></option> <#list workcenters as workcenter>
						<option value='${workcenter.workCenterCode}'>${workcenter.workCenterCode}-${workcenter.workCenterText}</option> </#list>
					</select> <label class="lb"><input type="checkbox" value="1"><@label key="submitAndReset.C,workCenter.C">提交后重置工作中心</@label></label>
				</p>
				<p id="storage">
					<label><@label key="workUnit.C,colon">作业单元：</@label></label> 
					<select id="workUnit" field="workUnitCode" name="workunitCode" label="workUnitSelectText">
					</select> 
					<label class="lb"><input type="checkbox" value="1"><@label key="submitAndReset.C,workUnit.C">提交后重置作业单元</@label></label>
				</p>
				<p>
					<label><@label key="productionControlNum.C,colon">生产管理号：</@label></label> 
					<input class="required look" id="productionControlLookup.productionControlNum" name="productionControlNum" type="text" postField="productionControlNum" warn="<@label key='productionControlNotInWorkcenter.M'>工作中心中不存在该生产管理号</@label>" remote="${rc.getContextPath()}/manage/productionControl/validateProductionControl" 
					suggestFields="productionControlNum" suggestUrl="${rc.getContextPath()}/manage/productionControl/productionControlSuggest" lookupGroup="productionControlLookup"/> 
					<a id="productionControlBtn" class="btnLook" href="${rc.getContextPath()}/manage/productionControl/productionControlLookUp" rel="materialLookup_dialog" title="<@label key='select.C,productionControlNum.C'>选择生产管理号</@label>" lookupGroup="productionControlLookup" /></a>
					<label class="lb"><input type="checkbox" value="1"><@label key="submitAndReset.C,productionControlNum.C">提交后重置生产管理号</@label></label>
				</p>
				<p>
					<label><@label key="material.C,colon">物料：</@label></label> 
					<input class="required look" id="materialLookup.materialCode" name="materialCode" type="text" postField="materialCode" suggestFields="materialCode,materialText,materialspec" suggestUrl="${rc.getContextPath()}/manage/material/componentSuggest" lookupGroup="materialLookup" callback="manualRequirementJs._mtChange" warn="<@label key='materialCode.C,doesNotExist.C'>物料编号不存在</@label>" remote="${rc.getContextPath()}/manage/material/validateMaterial" />
					 <a class="btnLook" href="${rc.getContextPath()}/manage/material/componentLookUp?lookupType=radio" rel="materialLookup_dialog" title="<@label key='select.C,materialCode.C'>选择物料</@label>" lookupGroup="materialLookup" callback="manualRequirementJs._mtChange"></a> 
					 <input style="margin-left: 15px;" id="materialLookup.materialText" name="materialText" type="text" readonly="readonly" /> 
					 <input id="materialLookup.materialspec" name="materialspec" type="hidden" readonly="readonly" /> 
					 <label class="lb"><input type="checkbox" value="1" checked><@label key="resetMaterial.C">提交后重置物料</@label></label>
				</p>
				<p>
					<label><@label key='quantity.C,colon'>数量：</@label></label> 
					<input id="requirementQuantity" type="text" name="requirementQuantity" class="required number" min="0.001" onkeyup="manualRequirementJs._checknum(this)" maxlength="15" /> 
					<label class="lb"><input type="checkbox" value="1" checked><@label key="resetQuantity.C">提交后重置数量</@label></label>
				</p>
				<p>
					<label><@label key='expectedArrivalDateTime.C,colon'>期望到货时间：</@label></label> 
					<input id="expectedArrivalDateTime" datefmt="yyyy-MM-dd HH:mm:ss" type="text" class="date required" name="expectedArrivalDateTime" value="${condition.expectedArrivalDateTime!''}" />
					<a class="inputDateButton" href="javascript:;">选择</a> 
					<label class="lb"><input type="checkbox" value="1" checked><@label key="submitAndReset.C,expectedArrivalDateTime.C">提交后重置期望到货时间</@label></label>
				</p>

				<div class="pageContent" style="padding: 5px 0;">
					<ul>
						<li>
							<div class="buttonActive" style="margin-left: 110px;">
								<div class="buttonContent">
									<button type="button" onclick="manualRequirementJs._submit(this)"><@label key="ok.B">确定</@label></button>
								</div>
							</div>
						</li>
						<li>
							<div class="buttonActiveGrey" style="margin-left: 50px;">
								<div class="buttonContent">
									<button type="reset" onclick='manualRequirementJs._reset(this)'><@label key="reset.B">重置</@label></button>
								</div>
							</div>
						</li>
					</ul>
				</div>
				<p>
				<label style="width: 108px"><@label key="opLog.C">操作日志：保留最近</@label></label> <input style="width: 50px" value="100" id="count" size="22" type="text" min="1" maxlength="5" class="integer number required" onkeypress="manualRequirementJs._countKeypress(event)" onblur="manualRequirementJs._countBlur()" /> <label><@label key="record.C">条记录</@label></label>
			</p>
			</form>

			


			<table class="table border" width="100%" layoutH="330" style="overflow: scroll;">
				<thead>
					<tr>
						<th width="100"><@label key="requirementNum.C">需求编号</@label></th>
						<th width="100"><@label key="productionControlNum.C">生产管理号</@label></th>
						<th width="100"><@label key="workCenter.C">工作中心</@label></th>
						<th width="100"><@label key="workUnit.C">作业单元</@label></th>
						<th width="100"><@label key="materialCode.C">物料编号</@label></th>
						<th width="100"><@label key="materialText.C">物料描述</@label></th>
						<th width="100"><@label key="materialspec.C">物料规格</@label></th>
						<th width="100"><@label key="requirementQuantity.C">需求数量</@label></th>
						<th width="100"><@label key="requirementCreate.C">需求产生时间</@label></th>
						<th width="100"><@label key="expectedArrivalDateTime.C">期望到货时间</@label></th>
					</tr>
				</thead>
				<tbody id='tbody'>
					<tr target="resource_id">
						<td colspan="7" class="noData">======<@label key="noListData.C">列表无数据</@label>=====</td>
					</tr>
				</tbody>
			</table>

		</div>
	</div>
</div>


<script type="text/javascript">
Date.prototype.Format = function(fmt) { //author: meizz   
    var o = {
        "M+" : this.getMonth() + 1, //月份   
        "d+" : this.getDate(), //日   
        "h+" : this.getHours(), //小时   
        "m+" : this.getMinutes(), //分   
        "s+" : this.getSeconds(), //秒   
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度   
        "S" : this.getMilliseconds()
    //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
                    : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
$(function(){  
	$('#materialLookup\\.materialCode',navTab.getCurrentPanel()).bind('input propertychange', function() {
		manualRequirementJs._mtChange();
	});
});  
var manualRequirementJs = {
    _isFirst:true,
    _mtChange:function(){
    	 var materialCode=$('#materialLookup\\.materialCode',navTab.getCurrentPanel()).val();
    	 DWZ.ajax({
             type:'POST',
             url:'${rc.getContextPath()}/manage/manualRequirement/mtChange',
             data:{'materialCode':materialCode},
             dataType:"json",
             cache: false,
             success: function(data){
            	 $('#requirementQuantity',navTab.getCurrentPanel()).val(data.requirementQuantity);
            	 if(data.expectedArrivalDateTime){
            		 $('#expectedArrivalDateTime',navTab.getCurrentPanel()).val(new Date(data.expectedArrivalDateTime).Format("yyyy-MM-dd hh:mm:ss"));
            	 }else{
            		 $('#expectedArrivalDateTime',navTab.getCurrentPanel()).val('');
            	 }
            	
             }
         });
    },
	_checknum: function(obj) {
		if (/^\d+\.?\d{0,3}$/.test(obj.value)) {
			obj.value = obj.value;
		} else {
			obj.value = obj.value.substring(0, obj.value.length - 1);
		}
	},
    _submit: function (t) {
        var $form = $(t).parents('form:first');
        if (!$form.valid()) {
            return false;
        }
		var pnum=$("#productionControlLookup\\.productionControlNum",navTab.getCurrentPanel()).val();
		if(pnum){
			var workcenterCode= $("#workcenter",navTab.getCurrentPanel()).val();
	    	var validateUrl="${rc.getContextPath()}/manage/productionControl/validateProductionControl";
	        if(workcenterCode){
	        	 validateUrl="${rc.getContextPath()}/manage/productionControl/validateProductionControl?workcenterCode="+workcenterCode;
	        }
			var result;
			$.ajax({
				type: 'POST',
				url: validateUrl,
				data: {
					productionControlNum: pnum
				},
				dataType: "text",
				async:false,
				success: function(data) {
					result = data;	
				},
				error: DWZ.ajaxError
			});
			if (result=="false") {
				return false;
			}
		}
        var d=$form.serializeArray();
        DWZ.ajax({
            type:'POST',
            url:'${rc.getContextPath()}/manage/manualRequirement/saveManualRequirement',
            data:d,
            dataType:"json",
            cache: false,
            success: function(data){
                // 根据checkbox重置数据
                $.each($('p > label > :checkbox', $form), function(){
                    if (this.checked) {
                        var $this = $(this), $p = $this.parent().parent();
                        $('select option:eq(0)', $p).attr('selected', 'selected').parent().change();
                        $('input[type=text]', $p).val('');
                    }
                });
               manualRequirementJs._addLog(d,data.data);
            }
        });
     },
     _workCenterChange:function(s)
     {
        var workcenterCode= $("#workcenter",navTab.getCurrentPanel()).val();
    	var validateUrl="${rc.getContextPath()}/manage/productionControl/validateProductionControl";
        var lookupUrl="${rc.getContextPath()}/manage/productionControl/productionControlLookUp";
        var suggestUrl="${rc.getContextPath()}/manage/productionControl/productionControlSuggest";
        if(workcenterCode){
        	 validateUrl="${rc.getContextPath()}/manage/productionControl/validateProductionControl?workcenterCode="+workcenterCode;
             lookupUrl="${rc.getContextPath()}/manage/productionControl/productionControlLookUp?workcenterCode="+workcenterCode;
             suggestUrl="${rc.getContextPath()}/manage/productionControl/productionControlSuggest?workcenterCode="+workcenterCode;
        }else{
        	$("#workUnit",navTab.getCurrentPanel()).empty();
        }
        $("#productionControlLookup\\.productionControlNum",navTab.getCurrentPanel()).attr("remote",validateUrl);
        $("#productionControlLookup\\.productionControlNum",navTab.getCurrentPanel()).attr("suggestUrl",suggestUrl);
        $("#productionControlBtn",navTab.getCurrentPanel()).attr("href",lookupUrl);
     },
     _reset:function(t)
     {
         var $form = $(t).parents('form:first');
            $.each($('p', $form), function(){
                var $p = $(this);
                $('select option:eq(0)', $p).attr('selected', 'selected').parent().change();
            });
     },
     _addLog:function(data,num)
     {
         var $tr = $('<tr/>');
         for(var i = 0; i < 10; i++) {
             $tr.append('<td/>');
         }
         $('td:eq(0)', $tr).append(num)
         $.each(data, function(){
             var key = this.name, value = this.value;
             if(key == 'productionControlNum') {
                 $('td:eq(1)', $tr).append(value)
             }else if(key == 'workcenterCode'){
                 $('td:eq(2)', $tr).append(value)
             }else if(key == 'workunitCode'){
                 $('td:eq(3)', $tr).append(value)
             }else if(key == 'materialCode'){
                 $('td:eq(4)', $tr).append(value)
             }else if(key == 'materialText'){
                 $('td:eq(5)', $tr).append(value)
             }else if(key == 'materialspec'){
                 $('td:eq(6)', $tr).append(value)
             }else if(key == 'requirementQuantity'){
                 $('td:eq(7)', $tr).append(value)
             }else if(key == 'expectedArrivalDateTime'){
                 $('td:eq(9)', $tr).append(value)
             }
         });
         $tbody=$('#tbody',navTab.getCurrentPanel());
         if(manualRequirementJs._isFirst)
         {
             $tbody.empty(); 
             manualRequirementJs._isFirst=false;
         }
         var mydate = new Date();
          var day = mydate.Format("yyyy-MM-dd hh:mm:ss");
         $('td:eq(8)', $tr).append(day)
         $tr.prependTo($tbody);
         var rowNums = parseInt($('#count',navTab.getCurrentPanel()).val()) || 100;
         if ($('tr', $tbody).length > rowNums) {
             $('tr:gt(' + (rowNums - 1) + ')', $tbody).remove();
         }
         $.jTableTool.initGridColumnWidth($tbody.parents('.grid:first'));
         if (!$('#count',navTab.getCurrentPanel()).val()) {
             $('#count',navTab.getCurrentPanel()).val(100);
         }
     },
     _countKeypress:function(event)
     {
         if(event.keyCode == "13")    
         {
             $tbody=$('#tbody',navTab.getCurrentPanel());
             var rowNums = parseInt($('#count',navTab.getCurrentPanel()).val()) || 100;
             if ($('tr', $tbody).length > rowNums) {
                 $('tr:gt(' + (rowNums - 1) + ')', $tbody).remove();
             }
         }
     },
     _countBlur:function()
     {
         $tbody=$('#tbody',navTab.getCurrentPanel());
         var rowNums = parseInt($('#count',navTab.getCurrentPanel()).val()) || 100;
         if ($('tr', $tbody).length > rowNums) {
             $('tr:gt(' + (rowNums - 1) + ')', $tbody).remove();
         }
     }
}
</script>
